package com.example.floristshop.adapters;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.floristshop.LoginActivity;
import com.example.floristshop.MainActivity;
import com.example.floristshop.R;
import com.example.floristshop.RecordFragment;
import com.example.floristshop.RecordInfoActivity;
import com.example.floristshop.database.Flower;
import com.example.floristshop.database.Record;
import com.example.floristshop.database.User;
import com.example.floristshop.utils.DBFindUtil;
import com.example.floristshop.utils.TimeUtil;

import org.litepal.LitePal;

import java.util.Collections;
import java.util.List;

public class RecordAdapter extends ArrayAdapter<Record> {

    private int record_item;

    public RecordAdapter(@NonNull Context context, int resource, @NonNull List<Record> objects) {
        super(context, resource, objects);
        record_item = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Record record = getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view = LayoutInflater.from(getContext()).inflate(record_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.flower_image_iv = (ImageView)view.findViewById(R.id.record_list_image);
            viewHolder.record_time_tv = (TextView)view.findViewById(R.id.record_list_time);
            viewHolder.flower_name_tv = (TextView)view.findViewById(R.id.record_list_flower_name);
            viewHolder.record_money_tv = (TextView)view.findViewById(R.id.record_list_money);
            viewHolder.record_num_tv = (TextView)view.findViewById(R.id.record_list_num);
            viewHolder.recorder_name_tv = (TextView)view.findViewById(R.id.record_list_user_name);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder)view.getTag();
        }
        Flower flower = DBFindUtil.findFlowerByFlowerId(record.getFlower_id());
        User user = DBFindUtil.findUserByUserId(record.getUser_id());
        viewHolder.flower_image_iv.setImageResource(flower.getImage());
        viewHolder.record_time_tv.setText(TimeUtil.timestamp_to_timeString(record.getTimestamp()));
        viewHolder.flower_name_tv.setText(flower.getName());
        viewHolder.record_money_tv.setText("￥"+record.getMoney());
        viewHolder.record_num_tv.setText("×"+record.getNum());
        viewHolder.recorder_name_tv.setText(user.getName());
        return view;
    }

    class ViewHolder{
        View recordView;
        ImageView flower_image_iv;
        TextView record_time_tv, flower_name_tv, record_money_tv, record_num_tv, recorder_name_tv;
    }
}
